package model;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

public class Ticket implements Serializable {
    private String id; // 电子票号，使用UUID
    private Screening screening;
    private int row;
    private int seat;
    private double price;
    private LocalDateTime saleTime;
    private TicketStatus status;

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public Ticket(Screening screening, int row, int seat) {
        this.id = UUID.randomUUID().toString();
        this.screening = screening;
        this.row = row;
        this.seat = seat;
        this.price = screening.getPrice();
        this.saleTime = LocalDateTime.now();
        this.status = TicketStatus.VALID;
    }

    public String getId() {
        return id;
    }

    public Screening getScreening() {
        return screening;
    }

    public int getRow() {
        return row;
    }

    public int getSeat() {
        return seat;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public LocalDateTime getSaleTime() {
        return saleTime;
    }

    public TicketStatus getStatus() {
        return status;
    }

    public void setStatus(TicketStatus status) {
        this.status = status;
    }

    public boolean canRefund() {
        // 电影开始前1小时内可以退票
        return status == TicketStatus.VALID && 
               LocalDateTime.now().plusHours(1).isBefore(screening.getStartTime());
    }

    @Override
    public String toString() {
        return "电子票号：" + id + "\n" +
               "电影：" + screening.getMovie().getTitle() + "\n" +
               "放映厅：" + screening.getHall().getName() + "\n" +
               "时间：" + screening.getStartTime().format(DATE_TIME_FORMATTER) + "\n" +
               "座位：" + (row + 1) + "排" + (seat + 1) + "座\n" +
               "票价：" + price + "元\n" +
               "状态：" + status;
    }
} 